using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Colorprj.Masters ;
using Colorprj.Common ;
namespace Colorprj
{
    public partial class frmBuyer : Form
      {

        BuyerManager bm = new BuyerManager();
        //public class ViewData
        //{
        //    private int m_Region;
        //    private int m_Territory;

        //    public ViewData(int Region, int Territory)
        //    {
        //        m_Region = Region;
        //        m_Territory = Territory;
        //    }

        //    public int RegionID
        //    {
        //        get { return m_Region; }
        //        set { m_Region = value; }
        //    }

        //    public int TerritoryID
        //    {
        //        get { return m_Territory; }
        //        set { m_Territory = value; }
        //    }
        //}


        //public List<ViewData> m_DataList;

        //DataSet RegionsTerritories = new DataSet();
        //DataTable regiontable = new DataTable("RegionTable");
        //DataTable territorytable = new DataTable("TerritoryTable");
        //BindingSource parentBS = new BindingSource();
        //BindingSource childBS = new BindingSource();
        //BindingSource filteredChildBS = new BindingSource();

        public  frmBuyer()
        {
            InitializeComponent();            
           
            //this.dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
            //this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            //this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
        }

        void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
           
        }       

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {           
            //if (this.dataGridView1.CurrentCell.ColumnIndex == territoryComboBoxColumn.Index)
            //{
            //    DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;
            //    BindingSource bs = control.DataSource as BindingSource;
            //    if (bs != null)
            //    {                   
            //        // set the filteredChildBS as the DataSource of the editing control
            //        (e.Control as ComboBox).DataSource = filteredChildBS;                    

            //        object regionvalue = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[regionComboBoxColumn.Index].Value;
            //        if (regionvalue == DBNull.Value || regionvalue == null)
            //        {
            //            filteredChildBS.Filter = "RegionID=-1";
            //        }
            //        else
            //        {
            //            filteredChildBS.Filter = "RegionID=" + regionvalue.ToString();
            //        }
            //        // set the SelectedValue property of the editing control
            //        if (this.dataGridView1.CurrentCell.Value != DBNull.Value && this.dataGridView1.CurrentCell.Value != null)
            //        {                       
            //            control.SelectedValue = this.dataGridView1.CurrentCell.Value;                                    
            //        }
            //    }
            //}
        }

       
        //void frm_Load(object sender, EventArgs e)
        //{
        //    // add columns in the Region table          
        //    DataColumn col = new DataColumn("RegionID", typeof(int));
        //    regiontable.Columns.Add(col);
        //    col = new DataColumn("RegionDescription");
        //    regiontable.Columns.Add(col);

        //    // add columns in the Territory table
        //    col = new DataColumn("TerritoryID", typeof(int));
        //    territorytable.Columns.Add(col);
        //    col = new DataColumn("TerritoryDescription");
        //    territorytable.Columns.Add(col);
        //    col = new DataColumn("RegionID", typeof(int));
        //    territorytable.Columns.Add(col);

        //    // add the two tables to the dataset
        //    RegionsTerritories.Tables.Add(regiontable);
        //    RegionsTerritories.Tables.Add(territorytable);

        //    // add some rows in the region table
        //    DataRow row = RegionsTerritories.Tables["RegionTable"].NewRow();
        //    row[0] = 3;
        //    row[1] = "region 3";
        //    RegionsTerritories.Tables["RegionTable"].Rows.Add(row);

        //    row = RegionsTerritories.Tables["RegionTable"].NewRow();
        //    row[0] = 2;
        //    row[1] = "region 2";
        //    RegionsTerritories.Tables["RegionTable"].Rows.Add(row);

        //    // add some rows in the Territory table
        //    row = RegionsTerritories.Tables["TerritoryTable"].NewRow();
        //    row[0] = 44122;
        //    row[1] = "terri 44122";
        //    row[2] = 3;
        //    RegionsTerritories.Tables["TerritoryTable"].Rows.Add(row);

        //    row = RegionsTerritories.Tables["TerritoryTable"].NewRow();
        //    row[0] = 53404;
        //    row[1] = "terri 53404";
        //    row[2] = 3;
        //    RegionsTerritories.Tables["TerritoryTable"].Rows.Add(row);

        //    row = RegionsTerritories.Tables["TerritoryTable"].NewRow();
        //    row[1] = "terri 60601";
        //    row[2] = 2;
        //    row[0] = 60601;
        //    RegionsTerritories.Tables["TerritoryTable"].Rows.Add(row);

        //    row = RegionsTerritories.Tables["TerritoryTable"].NewRow();
        //    row[0] = 94105;
        //    row[1] = "terri 94105";
        //    row[2] = 2;
        //    RegionsTerritories.Tables["TerritoryTable"].Rows.Add(row);           

        //    // set up ParentBS and ChildBS and filteredChildBS
        //    parentBS.DataSource = RegionsTerritories;
        //    parentBS.DataMember = "RegionTable";

        //    childBS.DataSource = RegionsTerritories;
        //    childBS.DataMember = "TerritoryTable";

        //    filteredChildBS.DataSource = RegionsTerritories;
        //    filteredChildBS.DataMember = "TerritoryTable";

        //    // bind the regionComboBoxColumn to the parentBS
        //    this.regionComboBoxColumn.DataSource = parentBS;
        //    this.regionComboBoxColumn.DisplayMember = "RegionDescription";
        //    this.regionComboBoxColumn.ValueMember = "RegionID";

        //    // bind the territoryComboBoxColumn to the childBS
        //    this.territoryComboBoxColumn.DataSource = childBS;
        //    this.territoryComboBoxColumn.DisplayMember = "TerritoryDescription";
        //    this.territoryComboBoxColumn.ValueMember = "TerritoryID";

        //    // Bound to a datatable
        //    DataTable dtViewData = new DataTable("ViewData");
        //    dtViewData.Columns.Add("RegionID", typeof(Int32));
        //    dtViewData.Columns.Add("TerritoryID", typeof(Int32));
        //    DataRow dr = dtViewData.NewRow();
        //    dr["RegionID"] = 3;
        //    dr["TerritoryID"] = 44122;
        //    dtViewData.Rows.Add(dr);
        //    dr = dtViewData.NewRow();
        //    dr["RegionID"] = 2;
        //    dr["TerritoryID"] = 60601;
        //    dtViewData.Rows.Add(dr);

        //    dataGridView1.AutoGenerateColumns = false;
        //    dataGridView1.DataSource = dtViewData;
        //    dataGridView1.Columns[0].DataPropertyName = "RegionID";
        //    dataGridView1.Columns[1].DataPropertyName = "TerritoryID";

        // }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //private void validateForm() {
 
        //}
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cboCrDr.SelectedIndex == -1)
            {
                MessageBox.Show("please select Cr/Dr");
                return; 
            }
            if (CboAccType.SelectedIndex == -1)
            {
                MessageBox.Show("please select AccType");
                return;
            }
            
            try
            {
           
                Buyer  pEntity = getFormData();
                bm.AddBuyer(pEntity);
                setAddMode();
                ClearForm(this.Controls);
                MessageBox.Show("Record Saved !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void ClearForm(Control.ControlCollection coll)
        {
            // Control.ControlCollection coll = this.Controls;
            foreach (Control c in coll)
            {
                if (c.HasChildren) ClearForm(c.Controls);
                if (c is TextBox) c.Text = string.Empty;
                //if (c is DataGridView) { ((DataGridView)c).Rows.Clear(); }
                if (c is ComboBox)
                {
                    ((ComboBox)c).SelectedIndex = -1;
                }
            }
        }
        private void setAddMode() {

            btnDelete.Enabled = false;
            btnUpdate.Enabled = false;
            btnSave.Enabled = true ;
            dataGridView1.DataSource = bm.GetAllBuyers().Tables[0] ; 
        
        }
        private void setEditDelMode()
        {

            btnDelete.Enabled = true;
            btnUpdate.Enabled = true ;
            btnSave.Enabled = false;

            
        }

        private void frmBuyer_Load(object sender, EventArgs e)
        {
            setAddMode();
            CboAccType.DataSource = bm.GetAllAccType().Tables[0];
            CboAccType.ValueMember  ="AccType";
            CboAccType.DisplayMember = "AccTypeDesc"; 
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            setEditDelMode();

            txtAddress1.Text = dataGridView1["BuyerAddress1", e.RowIndex].Value.ToString() ;
            txtAddress2.Text = dataGridView1["BuyerAddress2", e.RowIndex].Value.ToString();
            txtName.Text = dataGridView1["BuyerName", e.RowIndex].Value.ToString();
            txtId.Text = dataGridView1["Buyerid", e.RowIndex].Value.ToString();
            txtCity.Text = dataGridView1["BuyerCity", e.RowIndex].Value.ToString();
            txtPhone.Text = dataGridView1["PhoneNo", e.RowIndex].Value.ToString();
            txtEmail.Text = dataGridView1["EmailId", e.RowIndex].Value.ToString();
            txtTinNo.Text = dataGridView1["TinNo", e.RowIndex].Value.ToString();
            CboAccType.SelectedValue   =Utility.getIntVal(dataGridView1["AccType", e.RowIndex].Value.ToString());
            txtObBal.Text = dataGridView1["OpBal", e.RowIndex].Value.ToString();
            cboCrDr.SelectedItem = dataGridView1["DrCr", e.RowIndex].Value.ToString() ;
            if (cboCrDr.SelectedItem.Equals("Dr")) {
                txtObBal.Text = Convert.ToString((-1*Utility.getDblVal(dataGridView1["OpBal", e.RowIndex].Value.ToString())));
            }


        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void NumericKeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;


            if (!(char.IsDigit(e.KeyChar) | char.IsControl(e.KeyChar) | (e.KeyChar.Equals('.') & tb.Text.IndexOf(".") < 0)))
            {
                e.Handled = true;

            }


            if ((tb.SelectionStart > tb.Text.Length - 2 & tb.Text.IndexOf(".") >= 0 & tb.Text.IndexOf(".") + 3 == tb.Text.Length))
            {
                e.Handled = true;

            }


            if (e.Handled == false & tb.Text.Length == 0)
            {
                tb.Text = "" + e.KeyChar;

                e.Handled = true;

                tb.SelectionStart = 2;

            }

        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (cboCrDr.SelectedIndex == -1)
            {
                MessageBox.Show("please select Cr/Dr");
                return;
            }
            if (CboAccType.SelectedIndex == -1)
            {
                MessageBox.Show("please select AccType");
                return;
            }
            try
            {
                Buyer pEntity = getFormData();
                bm.UpdBuyer(pEntity);
                setAddMode();
                ClearForm(this.Controls);
                MessageBox.Show("Record Saved !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private Buyer getFormData()
        {
            Buyer pEntity = new Buyer();
           

            if (!string.IsNullOrEmpty(txtId.Text))
                pEntity.BuyerId = Convert.ToInt16(txtId.Text);
            pEntity.BuyerName = txtName.Text;
            pEntity.BuyerAddress1 = txtAddress1.Text;
            pEntity.BuyerAddress2 = txtAddress2.Text;
            pEntity.BuyerCity = txtCity.Text;
            pEntity.PhoneNo = txtPhone.Text;
            pEntity.EmailId = txtEmail.Text;

            pEntity.DrCr = cboCrDr.SelectedItem.ToString() ; 
            pEntity.AccType  = Utility.getIntVal(CboAccType.SelectedValue) ;
            if (pEntity.DrCr.Equals("Dr"))
            {
                pEntity.OpBal = -1 * Utility.getDblVal(txtObBal.Text);
            }
            else {
                pEntity.OpBal = Utility.getDblVal(txtObBal.Text);
            }
            pEntity.TinNo = txtTinNo.Text ;
            return pEntity;

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("Want To delete this record ? ", "", MessageBoxButtons.YesNo);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    Buyer pEntity = getFormData();
                    bm.DelBuyer(pEntity.BuyerId);
                    setAddMode();
                    ClearForm(this.Controls);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            setAddMode();
            ClearForm(this.Controls);
        }

    }

}